/*
 * @(#)CollectionPanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JTable;

import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.player.PlayerException;
import com.netexplode.jtunes.model.player.PlayerManager;

/**
 * <code>CollectionPanel</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.1 $
 * @since 0.1
 */
public abstract class CollectionPanel extends JPanel {

	private static final long serialVersionUID = 1L;

	protected boolean collectionLoaded;

	protected JTable[] tables;

	protected final String[] columnNames = { "artist", "album", "title" };

	protected CollectionManager collectionManager;

	protected PlayerManager playerManager;

	public void setCollectionManager(CollectionManager collectionManager) {
		this.collectionManager = collectionManager;
	}

	public void setPlayerManager(PlayerManager playerManager) {
		this.playerManager = playerManager;
	}

	abstract public void init();

	protected void addEventLister(final JTable tbl) {
		tbl.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if ((e.getButton() == MouseEvent.BUTTON1) && (e.getClickCount() == 2)) {
					Point point = e.getPoint();

					int row = tbl.rowAtPoint(point);

					collectionManager.getCurrrentCollection().setPosition(row);

					try {
						if (playerManager.isPlaying()) {
							playerManager.getMediaPlayer().stop();
						}
						playerManager.getMediaPlayer().play();
					}
					catch (PlayerException e1) {
						e1.printStackTrace();
					}
				}
			}
		});
	}

	protected void refresh() {
		if (!collectionLoaded) {
			this.init();
		}
	}

	abstract protected JTable getTable();
}